﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.Runtime.Serialization;
using System.Windows.Forms;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using OpenComposite.Workflow.Properties;
using System.Reflection;

namespace OpenComposite.Workflow
{
	[Designer(typeof(CompositeCapabilityDesigner))]
	[ToolboxItem(typeof(CompositeCapabilityToolboxItem))]
	[ToolboxBitmap(typeof(resfinder), "OpenComposite.Workflow.Resources.simulation.png")]
	public class CompositeCapabilityActivity : LogicalCapabilityActivity
	{
		public CompositeCapabilityActivity()
		{
		}

		public static DependencyProperty AssignedBusinessProcessProperty =
			DependencyProperty.Register("AssignedBusinessProcess", typeof(int), typeof(CompositeCapabilityActivity));

		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public int AssignedBusinessProcess
		{
			get { return (int)base.GetValue(AssignedBusinessProcessProperty); }
			set { base.SetValue(AssignedBusinessProcessProperty, value); }
		}

	}

	[ActivityDesignerTheme(typeof(CompositeCapabilityDesignerTheme))]
	public class CompositeCapabilityDesigner : LogicalCapabilityActivityDesigner
	{
		protected override void Initialize(Activity activity)
		{
			base.Initialize(activity);
			this.LogicalCapability.Type = LogicalCapabilityType.CompositeCapablity;
		}

		protected override void OnPaint(ActivityDesignerPaintEventArgs e)
		{
			if ( !IsVisible )
				return;

			base.OnPaint(e);
			//e.Graphics.DrawImage(Resources.Collapsed, ExpandButtonRectangle);
		}

		protected override void OnDragDrop(ActivityDragEventArgs e)
		{
			try {
				if ( e.Data.GetDataPresent(typeof(TreeNode)) ) {
					object item = ( (TreeNode)e.Data.GetData(typeof(TreeNode)) ).Tag;
					Type t = item.GetType();
					PropertyInfo piItemId = t.GetProperty("RepositoryItemType", BindingFlags.Instance | BindingFlags.Public);
					int rit = (int)piItemId.GetValue(item, null);
					if ( rit == 14 /*Verb*/ || rit == 8 /*Operation*/ ) {
						base.OnDragDrop(e);
					} else if ( rit == 58 /*Business Process*/ ) {
						PropertyInfo piId = t.GetProperty("ID", BindingFlags.Instance | BindingFlags.Public);
						int wfId = (int)piId.GetValue(item, null); //workflow (bp view) id
						( this.Activity as CompositeCapabilityActivity ).AssignedBusinessProcess = wfId;
					}
				}
			} catch ( Exception ex ) {
				MessageBox.Show(ex.ToString());
			}

			base.OnDragDrop(e);
		}

		protected override void OnDragOver(ActivityDragEventArgs e)
		{
			try {
				if ( e.Data.GetDataPresent(typeof(TreeNode)) ) {
					object item = ( (TreeNode)e.Data.GetData(typeof(TreeNode)) ).Tag;
					Type t = item.GetType();
					if ( t.Name == "Workflow" ) {
						PropertyInfo pi = t.GetProperty("Type", BindingFlags.Instance | BindingFlags.Public);
						int rit = (int)pi.GetValue(item, null); //WorkflowType
						if ( rit == 1 /* Business_Process*/ ) {
							e.Effect = DragDropEffects.Link;
						}
					}
				}
			} catch ( Exception ex ) {
				MessageBox.Show(ex.ToString());
			}
			base.OnDragOver(e);
		}

		protected override void OnMouseUp(MouseEventArgs e)
		{
			if ( e.Button == MouseButtons.Left && e.Clicks == 1 ) {
				// create composite
			}
			base.OnMouseUp(e);
		}

		public Rectangle ExpandButtonRectangle
		{
			get { return new Rectangle(Bounds.Right - 9, Bounds.Y + 1, 8, 8); }
		}

		protected override Rectangle ImageRectangle
		{
			get
			{
				Rectangle rc = base.ImageRectangle;
				rc.Offset(3, 0);
				return rc;
			}
		}
	}

	[Serializable]
	public class CompositeCapabilityToolboxItem : ActivityToolboxItem
	{
		public CompositeCapabilityToolboxItem(Type type)
			: base(type)
		{
			this.DisplayName = "Composite Capability";
		}

		private CompositeCapabilityToolboxItem(SerializationInfo info, StreamingContext context)
		{
			Deserialize(info, context);
		}
	}

	public class CompositeCapabilityDesignerTheme : LogicalCapabilityActivityDesignerTheme
	{
		public CompositeCapabilityDesignerTheme(WorkflowTheme theme)
			: base(theme)
		{
			this.BorderColor = Color.DarkBlue;
			this.BorderPen.Width = 1.5F;
		}
	}
}
